import { ref, reactive, computed } from 'vue';
import { onLoad, onShow, onHide, onPullDownRefresh, onPageScroll, onUnload } from '@dcloudio/uni-app';
import { img, handleOnloadParams } from '@/utils/common';
import { getDiyInfo } from '@/app/api/diy';
import useDiyStore from '@/app/stores/diy';
import { getByWebsite } from '@/app/api/auth';

export function useDiy(params: any = {}) {

    const loading = ref(true);
    const diyStore = useDiyStore();
    const pullDownRefreshCount = ref(0)

    const id = ref(params.id || 0)
    const name = ref(params.name || '')
    const template = ref('')
    let currRoute = "" //当前路由

    // 自定义页面 数据
    const diyData = reactive({
        pageMode: 'diy',
        title: '',
        global: {},
        value: []
    })

    const getLoading = () => {
        return loading.value;
    }

    const data: any = computed(() => {
        if (diyStore.mode == 'decorate') {
            return diyStore;
        } else {
            return diyData;
        }
    })

    const isShowTopTabbar = ref(false);

    const pageStyle = () => {
        var style = '';
        if (data.value.global.pageStartBgColor) {
            if (data.value.global.pageStartBgColor && data.value.global.pageEndBgColor) style += `background:linear-gradient(${data.value.global.pageGradientAngle},${data.value.global.pageStartBgColor},${data.value.global.pageEndBgColor});`;
            else style += 'background-color:' + data.value.global.pageStartBgColor + ';';
        }
        if (data.value.global.bottomTabBarSwitch) {
            style += 'min-height:calc(100vh - 50px);';
        } else {
            style += 'min-height:calc(100vh);';
        }
        if (data.value.global.bgUrl) {
            style += `background-image:url('${img(data.value.global.bgUrl)}');`;
        }

        if (data.value.global.bgHeightScale) {
            style += `background-size: 100% ${data.value.global.bgHeightScale}%;`;
        }

        return style;
    };

    // 监听页面加载
    const onLoadLifeCycle = () => {
        onLoad((option: any) => {
            // #ifdef MP-WEIXIN
            // 处理小程序场景值参数
            option = handleOnloadParams(option);
            // #endif

            // #ifdef H5
            // 装修模式
            diyStore.mode = option.mode || '';
            if (diyStore.mode == 'decorate') {
                loading.value = false;
            }
            // #endif


            if (id.value == 0) id.value = option.id || '';

            if (name.value == '') name.value = option.name || '';
            template.value = option.template || '';
        });
    }

    // 监听页面显示
    const onShowLifeCycle = (callback: any = null) => {
        onShow(() => {
            /******** 解决跳转自定义页面空白问题-第二步-start **********/
            let curPage: any = getCurrentPages();
            currRoute = curPage[curPage.length - 1] ? curPage[curPage.length - 1].route : ''; //获取当前页面的路由
            let urlArr = []
            if (uni.getStorageSync('diyPageBlank')) {
                urlArr = uni.getStorageSync('diyPageBlank');
            }
            if (!urlArr.length || urlArr.length && urlArr.indexOf(currRoute) == -1) {
                diyStore.topFixedStatus = 'home'
            } else if (urlArr.length && urlArr.indexOf(currRoute) != -1) {
                diyStore.topFixedStatus = 'diy'
            }
            /******** 解决跳转自定义页面空白问题-第二步-end **********/

            // 装修模式
            if (diyStore.mode == 'decorate') {
                diyStore.init();
            } else {

                if (uni.getStorageSync('wap_site_id')) {
                    getDiyInfoFn(callback)

                } else {
                    // 获取站点id 
                    let wap_site_id = ''
                    let domain = ''
                    // #ifdef H5
                    domain = location.hostname
                    // #endif
                    // #ifdef MP-WEIXIN
                    const accountInfo = uni.getAccountInfoSync();
                    domain = accountInfo.miniProgram.appId
                    // #endif
                    wap_site_id = '1'
                    uni.setStorageSync('wap_site_id', wap_site_id)
                    getDiyInfoFn(callback)
                    // getByWebsite({ website: domain }).then((res: any) => {
                    //     wap_site_id = res.data.id
                    //     uni.setStorageSync('wap_site_id', wap_site_id)
                    //     getDiyInfoFn(callback)
                    // })


                }

            }

        })
    }
    const getDiyShopMemberUserIndex = (callback: any) => {
        let data = {
            "site_id": 1,
            "title": "会员信息",
            "name": "DIY_SHOP_MEMBER_USER_INDEX",
            "type": "DIY_SHOP_MEMBER_USER_INDEX",
            "template": "shop_member_index_style1",
            "mode": "diy",
            "value": "{\"global\":{\"title\":\"会员信息\",\"bgUrl\":\"\",\"imgWidth\":\"\",\"imgHeight\":\"\",\"bottomTabBarSwitch\":false,\"template\":{\"textColor\":\"#303133\",\"componentStartBgColor\":\"\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":0,\"both\":0},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\"},\"topStatusBar\":{\"isShow\":false,\"bgColor\":\"#ffffff\",\"rollBgColor\":\"#ffffff\",\"style\":\"style-1\",\"styleName\":\"风格1\",\"textColor\":\"#333333\",\"rollTextColor\":\"#333333\",\"textAlign\":\"center\",\"inputPlaceholder\":\"请输入搜索关键词\",\"imgUrl\":\"\",\"link\":{\"name\":\"\"}},\"popWindow\":{\"imgUrl\":\"\",\"imgWidth\":\"\",\"imgHeight\":\"\",\"count\":-1,\"show\":0,\"link\":{\"name\":\"\"}},\"pageStartBgColor\":\"rgba(246, 246, 246, 1)\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"bgHeightScale\":0},\"value\":[{\"path\":\"edit-shop-member-info\",\"id\":\"3pt9pn9bvn20\",\"componentName\":\"MemberInfo\",\"componentTitle\":\"会员信息\",\"uses\":1,\"ignore\":[\"componentBgUrl\"],\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"rgba(255, 255, 255, 1)\",\"topRounded\":10,\"bottomRounded\":10,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":10,\"bottom\":0,\"both\":10},\"textColor\":\"#333333\",\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\",\"style\":\"style-1\",\"styleName\":\"风格1\",\"bgUrl\":\"\",\"isShowAccount\":false},{\"path\":\"edit-horz-blank\",\"uses\":0,\"id\":\"6cil3ipiis40\",\"componentName\":\"HorzBlank\",\"componentTitle\":\"辅助空白\",\"ignore\":[\"pageBgColor\",\"componentBgUrl\"],\"height\":10,\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":12,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":10,\"bottom\":0,\"both\":10},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\"},{\"path\":\"edit-text\",\"uses\":0,\"id\":\"68gnubbygqg0\",\"componentName\":\"Text\",\"componentTitle\":\"标题\",\"ignore\":[],\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":0,\"both\":10},\"fontSize\":15,\"fontWeight\":\"normal\",\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\",\"position\":\"\",\"style\":\"style-1\",\"styleName\":\"风格1\",\"text\":\"订单中心\",\"link\":{\"name\":\"\"},\"textAlign\":\"left\",\"subTitle\":{\"text\":\"副标题\",\"color\":\"#999999\",\"fontSize\":14,\"control\":false,\"fontWeight\":\"normal\"},\"more\":{\"text\":\"查看更多\",\"control\":false,\"isShow\":true,\"link\":{\"name\":\"\"},\"color\":\"#999999\"}},  {\"path\":\"edit-graphic-nav\",\"uses\":0,\"id\":\"5myxk1opz0c0\",\"componentName\":\"GraphicNav\",\"componentTitle\":\"图文导航\",\"ignore\":[],\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":12,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":0,\"both\":10},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\",\"layout\":\"horizontal\",\"mode\":\"graphic\",\"showStyle\":\"fixed\",\"rowCount\":4,\"pageCount\":2,\"carousel\":{\"type\":\"circle\",\"color\":\"#FFFFFF\"},\"imageSize\":20,\"aroundRadius\":4,\"font\":{\"size\":16,\"weight\":\"normal\",\"color\":\"#303133\"},\"list\":[{\"id\":\"49kxbgr5lwue\",\"title\":\"销售订单\",\"imageUrl\":\"addon/shop/diy/member/style1/nav_sign_in.png\",\"imgWidth\":92,\"imgHeight\":92,\"link\":{\"name\":\"MEMBER_MY_SIGN_IN\",\"parent\":\"MEMBER_LINK\",\"title\":\"我的签到\",\"url\":\"/addon/shop/pages/shopOrder/list?type=member\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"49kxbgr5lwue\",\"title\":\"服务订单\",\"imageUrl\":\"addon/shop/diy/member/style1/nav_sign_in.png\",\"imgWidth\":92,\"imgHeight\":92,\"link\":{\"name\":\"MEMBER_MY_SIGN_IN\",\"parent\":\"MEMBER_LINK\",\"title\":\"我的签到\",\"url\":\"/addon/service/pages/order/list?type=member\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"title\":\"寄存订单\",\"link\":{\"parent\":\"DIY_LINK\",\"name\":\"DIY_LINK\",\"title\":\"寄存订单\",\"url\":\"/addon/deposit/pages/order/list?type=member\",\"action\":\"\"},\"imageUrl\":\"addon/shop/diy/member/style1/nav_balance.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"2aqy33banse8\",\"imgWidth\":92,\"imgHeight\":92},{\"title\":\"提货订单\",\"link\":{\"parent\":\"DIY_LINK\",\"name\":\"DIY_LINK\",\"title\":\"提货订单\",\"url\":\"/addon/pickUp/pages/order/list?type=member\",\"action\":\"\"},\"imageUrl\":\"addon/shop/diy/member/style1/nav_address.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"hdk4145zk40\",\"imgWidth\":92,\"imgHeight\":92},{\"title\":\"欠款订单\",\"link\":{\"parent\":\"DIY_LINK\",\"name\":\"DIY_LINK\",\"title\":\"欠款订单\",\"url\":\"/addon/debt/pages/order/list?type=member\",\"action\":\"\"},\"imageUrl\":\"addon/shop/diy/member/style1/nav_coupon.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"34t5xg9ik0i0\",\"imgWidth\":92,\"imgHeight\":92},{\"id\":\"7dpefp0s9ew0\",\"title\":\"还款订单\",\"imageUrl\":\"addon/shop/diy/member/style1/nav_promote.png\",\"imgWidth\":92,\"imgHeight\":92,\"link\":{\"name\":\"DIY_LINK\",\"parent\":\"DIY_LINK\",\"title\":\"还款订单\",\"url\":\"/addon/refund/pages/order/list?type=member\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"78kxbgr4lipw\",\"title\":\"核销订单\",\"imageUrl\":\"addon/shop/diy/member/style1/nav_verify_index.png\",\"imgWidth\":92,\"imgHeight\":92,\"link\":{\"name\":\"MEMBER_VERIFY_INDEX\",\"parent\":\"MEMBER_LINK\",\"title\":\"核销台\",\"url\":\"/app/pages/verify/record?type=member\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"27kxbgr5lj3w\",\"title\":\"充值订单\",\"imageUrl\":\"addon/shop/diy/member/style1/nav_giftcard.png\",\"imgWidth\":92,\"imgHeight\":92,\"link\":{\"name\":\"SHOP_GIFTCARD_MY_CARD_LIST\",\"parent\":\"SHOP_GIFTCARD_LINK\",\"title\":\"我的卡包\",\"url\":\"/addon/recharge/pages/recharge_record?type=member\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}}],\"swiper\":{\"indicatorColor\":\"rgba(0, 0, 0, 0.3)\",\"indicatorActiveColor\":\"#FF0E0E\",\"indicatorStyle\":\"style-1\",\"indicatorAlign\":\"center\"}}, {\"path\":\"edit-horz-blank\",\"uses\":0,\"id\":\"6cil3ipiis40\",\"componentName\":\"HorzBlank\",\"componentTitle\":\"辅助空白\",\"ignore\":[\"pageBgColor\",\"componentBgUrl\"],\"height\":10,\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":12,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":10,\"bottom\":0,\"both\":10},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\"},{\"path\":\"edit-text\",\"uses\":0,\"id\":\"68gnubbygqg0\",\"componentName\":\"Text\",\"componentTitle\":\"标题\",\"ignore\":[],\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":0,\"both\":10},\"fontSize\":15,\"fontWeight\":\"normal\",\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\",\"position\":\"\",\"style\":\"style-1\",\"styleName\":\"风格1\",\"text\":\"其他功能\",\"link\":{\"name\":\"\"},\"textAlign\":\"left\",\"subTitle\":{\"text\":\"副标题\",\"color\":\"#999999\",\"fontSize\":14,\"control\":false,\"fontWeight\":\"normal\"},\"more\":{\"text\":\"查看更多\",\"control\":false,\"isShow\":true,\"link\":{\"name\":\"\"},\"color\":\"#999999\"}},{\"path\":\"edit-horz-blank\",\"uses\":0,\"id\":\"5jx57rrjl940\",\"componentName\":\"HorzBlank\",\"componentTitle\":\"辅助空白\",\"ignore\":[\"pageBgColor\",\"componentBgUrl\"],\"height\":8,\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":0,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":0,\"both\":10},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\"},{\"path\":\"edit-graphic-nav\",\"uses\":0,\"id\":\"5myxk1opz0c0\",\"componentName\":\"GraphicNav\",\"componentTitle\":\"图文导航\",\"ignore\":[],\"textColor\":\"#303133\",\"componentStartBgColor\":\"rgba(255, 255, 255, 1)\",\"componentEndBgColor\":\"\",\"topRounded\":0,\"bottomRounded\":12,\"elementBgColor\":\"\",\"topElementRounded\":0,\"bottomElementRounded\":0,\"margin\":{\"top\":0,\"bottom\":10,\"both\":10},\"pageStartBgColor\":\"\",\"pageEndBgColor\":\"\",\"pageGradientAngle\":\"to bottom\",\"componentBgUrl\":\"\",\"componentBgAlpha\":2,\"componentGradientAngle\":\"to bottom\",\"layout\":\"horizontal\",\"mode\":\"graphic\",\"showStyle\":\"fixed\",\"rowCount\":4,\"pageCount\":2,\"carousel\":{\"type\":\"circle\",\"color\":\"#FFFFFF\"},\"imageSize\":48,\"aroundRadius\":8,\"font\":{\"size\":16,\"weight\":\"normal\",\"color\":\"#303133\"},\"list\":[{\"title\":\"销售\",\"link\":{\"parent\":\"SHOP_LINK\",\"title\":\"商品分类\",\"url\":\"/addon/shop/pages/goods/category\",\"name\":\"SHOP_GOODS_CATEGORY\",\"action\":\"\"},\"imageUrl\":\"https://files.meton.vip/41b57aab3360ea1282cb93ab7b04446d15c90d9f28f59b938279ad6932262203.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"4dha169dzqe0\",\"imgWidth\":50,\"imgHeight\":50},{\"title\":\"服务\",\"link\":{\"parent\":\"DIY_LINK\",\"title\":\"服务\",\"url\":\"/addon/service/pages/goods/category\",\"name\":\"DIY_LINK\",\"action\":\"\"},\"imageUrl\":\"https://files.meton.vip/94e077b1f18d7031867fc5a9b2c44a64c8912d6e0808e0a0843476ca1c49a443.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"7eh2hqz0w900\",\"imgWidth\":50,\"imgHeight\":50},{\"id\":\"7fu7atbr9bk0\",\"title\":\"充值\",\"imageUrl\":\"https://files.meton.vip/76142dae98e89304c1cc086105650367b2789d5017821ee7d8f066bc0525df72.png\",\"imgWidth\":50,\"imgHeight\":50,\"link\":{\"parent\":\"SHOP_LINK\",\"title\":\"我的余额\",\"url\":\"/app/pages/member/balance\",\"name\":\"MEMBER_BALANCE\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"title\":\"寄存\",\"link\":{\"parent\":\"DIY_LINK\",\"url\":\"/addon/deposit/pages/goods/category\",\"title\":\"寄存\",\"name\":\"DIY_LINK\",\"action\":\"\"},\"imageUrl\":\"https://files.meton.vip/37b3cf4dca1cb5d7d2442849ea7bb5ed214414552a2448ab1671a2ad6a422ff4.png\",\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"},\"id\":\"5q9ogqz2vjc0\",\"imgWidth\":50,\"imgHeight\":50},{\"id\":\"2cc20cawafr4\",\"title\":\"提货\",\"imageUrl\":\"https://files.meton.vip/d7bf96863ea51b318597cfc14841dcdd1cc449d339acce42a7ad9f37a783de53.png\",\"imgWidth\":50,\"imgHeight\":50,\"link\":{\"parent\":\"DIY_LINK\",\"title\":\"提货\",\"url\":\"/addon/pickUp/pages/goods/category\",\"name\":\"DIY_LINK\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"532sx2po4do0\",\"title\":\"体重管理\",\"imageUrl\":\"https://files.meton.vip/2027ce0ffe291fce77e9f54bb4b63a9dade2cf5283edb68b451297a1b3d62211.png\",\"imgWidth\":50,\"imgHeight\":50,\"link\":{\"name\":\"DIY_LINK\",\"parent\":\"DIY_LINK\",\"title\":\"体重管理\",\"url\":\"/addon/weight/pages/index\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"532sx2po4do0\",\"title\":\"剩余卡项\",\"imageUrl\":\"https://files.meton.vip/3efa9ad9dcedf5e2ff7525c1bfacfcc894df0569866803ce4746c1bcc8e14336.png\",\"imgWidth\":50,\"imgHeight\":50,\"link\":{\"name\":\"DIY_LINK\",\"parent\":\"DIY_LINK\",\"title\":\"剩余卡项\",\"url\":\"/addon/shopMemberInfo/pages/spuCard_detail\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}},{\"id\":\"532sx2po4do0\",\"title\":\"核销\",\"imageUrl\":\"https://files.meton.vip/8b05a037bbcbd7d9a658a2cdeeefde1a4fe28152a6a37d5228486723032a324d.png\",\"imgWidth\":50,\"imgHeight\":50,\"link\":{\"name\":\"DIY_LINK\",\"parent\":\"DIY_LINK\",\"title\":\"核销\",\"url\":\"/app/pages/verify/index\",\"action\":\"\"},\"label\":{\"control\":false,\"text\":\"热门\",\"textColor\":\"#FFFFFF\",\"bgColorStart\":\"#F83287\",\"bgColorEnd\":\"#FE3423\"}}],\"swiper\":{\"indicatorColor\":\"rgba(0, 0, 0, 0.3)\",\"indicatorActiveColor\":\"#FF0E0E\",\"indicatorStyle\":\"style-1\",\"indicatorAlign\":\"center\"}}]}",
            "is_default": 1,
            "share": "",
            "visit_count": 0
        }
        let requestData = data;
        if (requestData.value) {
            diyData.pageMode = requestData.mode;
            diyData.title = requestData.title;

            let sources = JSON.parse(requestData.value);
            diyData.global = sources.global;
            diyData.value = sources.value;
            diyData.value.forEach((item: any, index) => {
                item.pageStyle = '';
                if (item.pageStartBgColor) {
                    if (item.pageStartBgColor && item.pageEndBgColor) item.pageStyle += `background:linear-gradient(${item.pageGradientAngle},${item.pageStartBgColor},${item.pageEndBgColor});`;
                    else item.pageStyle += 'background-color:' + item.pageStartBgColor + ';';
                }

                if (item.margin) {
                    if (item.margin.top > 0) {
                        item.pageStyle += 'padding-top:' + item.margin.top * 2 + 'rpx' + ';';
                    }
                    item.pageStyle += 'padding-bottom:' + item.margin.bottom * 2 + 'rpx' + ';';
                    item.pageStyle += 'padding-right:' + item.margin.both * 2 + 'rpx' + ';';
                    item.pageStyle += 'padding-left:' + item.margin.both * 2 + 'rpx' + ';';
                }
            });

            // 控制自定义头部是否出现 | 微信小程序
            isShowTopTabbar.value = diyData.value.some((item: any) => {
                return item && item.position && item.position == 'top_fixed'
            });

            uni.setNavigationBarTitle({
                title: diyData.title
            });

        }

        loading.value = false;

        if (callback) callback(requestData)
    }//获取app 会员中心自定义页面配置 目前先写死后期扩展
    const getDiyInfoFn = (callback: any) => {
        // if (name.value == 'DIY_SHOP_MEMBER_USER_INDEX') {
        //     getDiyShopMemberUserIndex(callback)
        //     return
        // }
        getDiyInfo({
            id: id.value,
            name: name.value,
            template: template.value
        }).then((res: any) => {
            let requestData = res.data;
            if (requestData.value) {
                diyData.pageMode = requestData.mode;
                diyData.title = requestData.title;

                let sources = JSON.parse(requestData.value);
                diyData.global = sources.global;
                diyData.value = sources.value;
                diyData.value.forEach((item: any, index) => {
                    item.pageStyle = '';
                    if (item.pageStartBgColor) {
                        if (item.pageStartBgColor && item.pageEndBgColor) item.pageStyle += `background:linear-gradient(${item.pageGradientAngle},${item.pageStartBgColor},${item.pageEndBgColor});`;
                        else item.pageStyle += 'background-color:' + item.pageStartBgColor + ';';
                    }

                    if (item.margin) {
                        if (item.margin.top > 0) {
                            item.pageStyle += 'padding-top:' + item.margin.top * 2 + 'rpx' + ';';
                        }
                        item.pageStyle += 'padding-bottom:' + item.margin.bottom * 2 + 'rpx' + ';';
                        item.pageStyle += 'padding-right:' + item.margin.both * 2 + 'rpx' + ';';
                        item.pageStyle += 'padding-left:' + item.margin.both * 2 + 'rpx' + ';';
                    }
                });

                // 控制自定义头部是否出现 | 微信小程序
                isShowTopTabbar.value = diyData.value.some((item: any) => {
                    return item && item.position && item.position == 'top_fixed'
                });

                uni.setNavigationBarTitle({
                    title: diyData.title
                });

            }

            loading.value = false;

            if (callback) callback(requestData)

        });

    }
    // 监听页面隐藏
    const onHideLifeCycle = (callback: any = null) => {
        onHide(() => {
            /******** 解决跳转自定义页面空白问题-第一步 -start **********/
            let url = [];
            if (uni.getStorageSync('diyPageBlank')) {
                url = uni.getStorageSync('diyPageBlank');
            }

            // 清空重复、与当前页面路径一致的url
            if (url.length) {
                url = Array.from(new Set(url))
                url.forEach((item, index, arr) => {
                    if (item == currRoute) {
                        arr.splice(index, 1);
                    }
                })
            }

            // 当diyStore.topFixedStatus == "diy"时,存储到diyPageBlank缓存中
            if (diyStore.topFixedStatus == "diy") {
                url.push(currRoute);
            }
            uni.setStorageSync('diyPageBlank', url);
            /******** 解决跳转自定义页面空白问题-第一步 -end **********/

            if (callback) callback()
        })
    }

    // 监听页面卸载
    const onUnloadLifeCycle = () => {
        onUnload(() => {

        })
    }

    // 监听下拉刷新事件
    const onPullDownRefreshLifeCycle = () => {
        onPullDownRefresh(() => {
            pullDownRefreshCount.value++;
            uni.stopPullDownRefresh();
        })
    }

    // 监听滚动事件
    const onPageScrollLifeCycle = () => {
        onPageScroll((e) => {
            if (e.scrollTop > 0) {
                diyStore.scrollTop = e.scrollTop;
            }
        })
    }

    return {
        getLoading,
        pullDownRefreshCount,
        data: data.value,
        isShowTopTabbar,
        pageStyle,
        onLoad: onLoadLifeCycle,
        onShow: onShowLifeCycle,
        onHide: onHideLifeCycle,
        onUnload: onUnloadLifeCycle,
        onPullDownRefresh: onPullDownRefreshLifeCycle,
        onPageScroll: onPageScrollLifeCycle,
    }
}
